package machines

import (
	"cloud_android_backend/api/common"
	"cloud_android_backend/pkg/models/resources"
	"cloud_android_backend/pkg/modules/machines"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/golang/glog"
	rb "modules.tenxcloud.com/common/middleware"
	"net/http"
)

func Register(application *gin.RouterGroup) {
	application.POST("/machines/add", rb.RequestBody, AddMachine)
}
func AddMachine(c *gin.Context) {
	body, _ := c.Get(rb.RequestBodyKey)
	orderForm := resources.Machines{}
	if err := json.Unmarshal(body.([]byte), &orderForm); err != nil {
		glog.Error(err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	err := machines.CreateMachine(&orderForm)
	if err != nil {
		glog.Error(err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	c.JSON(http.StatusOK, common.SuccessResponse("success"))
}
